package cn.itcast.nems.studentrecord.web.controller.b;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.lang.Assert;
import cn.itcast.ic.common.bean.Page;
import cn.itcast.ic.common.exception.v2.BizExceptionProducer;
import cn.itcast.nems.common.PageQueryModel;
import cn.itcast.nems.common.enumeration.NemsErrorBodyEnum;
import cn.itcast.nems.journal.dto.JournalFinalLineQueryDTO;
import cn.itcast.nems.journal.service.JournalFinalLineService;
import cn.itcast.nems.manager.util.CustomBeanUtil;
import cn.itcast.nems.manager.util.RequestToEntityConverter;
import cn.itcast.nems.studentrecord.dao.dataobject.JournalFinalLineDO;
import cn.itcast.nems.studentrecord.web.vo.response.JournalFinalLinePageVO;
import com.baomidou.mybatisplus.core.metadata.IPage;
import jakarta.servlet.http.HttpServletRequest;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 结课凭单-行 前端控制器
 * </p>
 *
 * @author zhouwei
 * @since 2024/1/11 4:40 PM
 **/
@RestController
@RequiredArgsConstructor
@RequestMapping("/b/journal/final")
public class JournalFinalLineController {
    private final JournalFinalLineService journalFinalLineService ;
    /**
     * <p>
     * 分页查询
     * </p>
     *
     * @param pageNumber 页码
     * @param pageSize 每页显示最大条目
     * @author zhouwei
     * @since 2024/1/11 11:05 AM
     **/
    @GetMapping(value = "{journal_id}/lines")
    public ResponseEntity<Page<JournalFinalLinePageVO>> list(HttpServletRequest request,
                                                             @PathVariable(name = "journal_id") String journalFinalId ,
                                                             @RequestParam(value = "page_number", required = false) Integer pageNumber,
                                                             @RequestParam(value = "page_size", required = false) Integer pageSize) {
        Assert.notNull(journalFinalId , BizExceptionProducer.produce(NemsErrorBodyEnum.PARAM_EMPTY ,"结课凭单ID")) ;
        JournalFinalLineQueryDTO queryVO = (JournalFinalLineQueryDTO) RequestToEntityConverter.convert(request, JournalFinalLineQueryDTO.class);
        queryVO.setJournalFinalId(journalFinalId) ;
        PageQueryModel<JournalFinalLineQueryDTO> queryModel = new PageQueryModel<>(pageNumber, pageSize, queryVO);
        queryModel.convert();
        IPage<JournalFinalLineDO> pageData = journalFinalLineService.findPageList(queryModel);
        List<JournalFinalLinePageVO> list = Objects.isNull(pageData ) || CollectionUtil.isEmpty(pageData.getRecords()) ?
                null :
                pageData.getRecords().stream().map(this::transferToJournalFinalLinePageVO).toList();
        return ResponseEntity.ok().body(
                Page.of(
                        queryModel.getPageNumber(),
                        queryModel.getPageSize(),
                        Long.valueOf(pageData.getTotal()).intValue(),
                        list
                )
        );
    }
    private JournalFinalLinePageVO transferToJournalFinalLinePageVO(JournalFinalLineDO lineDO) {
        return CustomBeanUtil.copyProperties(lineDO ,new JournalFinalLinePageVO()) ;
    }
}
